package com.topofstack.flp.ui;

import com.bappi.custom.activities.CustomActivity;
import com.topofstack.flp.FLPApplication;
import com.topofstack.flp.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class NoteComposeActivity extends CustomActivity  implements OnClickListener {
	
	EditText txtTitle, txtNote;
	Button btnSave, btnDelete, btnCancel;
	String name;
	FLPApplication application;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_note_compose);
		
		application = (FLPApplication)getApplication();
		
		txtTitle = (EditText)findViewById(R.id.txtTitle);
		txtNote = (EditText)findViewById(R.id.txtNote);
		
		btnSave = (Button)findViewById(R.id.btnSave);
		btnSave.setOnClickListener(this);
		
		btnDelete = (Button)findViewById(R.id.btnDelete);
		btnDelete.setOnClickListener(this);
		
		btnCancel = (Button)findViewById(R.id.btnCancel);
		btnCancel.setOnClickListener(this);
		
		try{
			name = getIntent().getExtras().getString("name");
		}
		catch (Exception e) {
			name = null;
		}
		
		if(name == null){
			//create
			btnDelete.setVisibility(View.INVISIBLE);
		}
		else{
			//update
			txtTitle.setEnabled(false);
			String []partz = name.split("___");
			txtTitle.setText(partz[0]);
			String contents = application.getAssetManager().getNoteContents(name);
			txtNote.setText(contents);
		}
	}

	@Override
	public void onClick(View sender) {
		if(sender == btnSave){
			if(name == null){
				//create
				application.getAssetManager().saveNoteContents(txtNote.getText().toString(), txtTitle.getText().toString());
			}
			else{
				//update
				application.getAssetManager().deleteNote(name);
				application.getAssetManager().saveNoteContents(txtNote.getText().toString(), txtTitle.getText().toString());
			}
		}
		else if(sender == btnDelete){
			application.getAssetManager().deleteNote(name);
		}
		else if(sender == btnCancel){

		}

		finish();
	}
}
